<?php
require_once("../../Config.php");
require_once("../../Class/ClassFunc.php");
require_once("../../Class/ClassMysql.php");
require_once("../../Class/ClassCheck.php");
require_once("../../Class/ClassResult.php");

//json头
header("Content-type: application/json");

session_start();
if(!isset($_SESSION["name"])) {
    exit(Result::jsonReturn(1, "未登录"));
}

$uid = $_SESSION["id"];
$uname = $_SESSION["name"];
$uemail = $_SESSION["email"];
$upower = $_SESSION["power"];


// 校验是否为空
if(Check::isEmpty($_POST["title"])) {
    exit(Result::jsonReturn(1, "名称不得为空"));
}
if(Check::isEmpty($_POST["slug"])) {
    exit(Result::jsonReturn(1, "简略名不得为空"));
}


$info = array();
// 校验参数合法性
if(Check::isName($_POST["title"])) {
    $info["title"] = $_POST["title"];
}else {
    exit(Result::jsonReturn(1, "名称不符合规定"));
}
if(Check::isSlug($_POST["slug"])) {
    $info["slug"] = $_POST["slug"];
}else {
    exit(Result::jsonReturn(1, "简略名只能为英文字母、数字、下划线"));
}
if($_POST["checked"] != NULL) {
    foreach($_POST["checked"] as $value) {
        if(!Check::isNum($value) || (int)$value==0) {
            exit(Result::jsonReturn(1, "选择列表参数不正确"));
        }
    }
    $info["belong"] = $_POST["checked"];
}
$info["text"] = Check::checkPost("在这里编写文章");
$info["type"] = 1;

// 构造sql语句
$db = new Db();
$sql = "INSERT INTO qilian_contents(user_id, user_name, title, slug, `text`, type)".
        " VALUES({$uid}, '{$uname}', '{$info["title"]}', '{$info["slug"]}', '{$info["text"]}', {$info["type"]})";
$id = 0;
if($id=$db->insert($sql)) {
    if(isset($info["belong"])) {
        foreach($info["belong"] as $value) {
            $sql_re = "INSERT INTO qilian_relationships(content_id, list_id) VALUES({$id}, {$value})";
            $db->insert($sql_re);
        }        
    }
    $sql_sel = "SELECT * FROM qilian_contents WHERE id={$id}";
    $res_sel = array();
    if($res_sel = $db->getOne($sql_sel)) {
        $nameArr = array();
        if(isset($info["belong"])) {
            foreach($info["belong"] as $val) {
                $sql_l = "SELECT name FROM qilian_lists WHERE id={$val}";
                array_push($nameArr, $db->getOne($sql_l)["name"]);
            }
            $res_sel["belong"] = $info["belong"];
        }else {
            $res_sel["belong"] = array();
        }
        $res_sel["belongname"] = $nameArr; 
        $res_sel["attachments"] = array();
        exit(Result::jsonReturn(0, "添加文章成功", $res_sel));
    }else {
        $nameArr = array();
        $idArr = array();
        if(isset($info["belong"])) {
            foreach($info["belong"] as $val) {
                $sql_l = "SELECT name FROM qilian_lists WHERE id={$val}";
                array_push($nameArr, $db->getOne($sql_l)["name"]);
            } 
            $idArr = $info["belong"];
        }
        exit(Result::jsonReturn(0, "添加文章成功", array(
            "id" => $id,
            "user_id" => $uid,
            "user_name" => $uname,
            "title" => $info["title"],
            "slug" => $info["slug"],
            "text" => $info["text"],
            "type" => 1,
            "belong" => $idArr,
            "belongname" => $nameArr,
            "attachments" => array(),
            "created_time" => Func::getTime(),
            "updated_time" => Func::getTime())));        
    }

}else {
    exit(Result::jsonReturn(1, "添加文章失败"));
}



?>